HTMLify
Help Classmates.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | // Help Classmates gfg problem import java.util.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t > 0){ int n = sc.nextInt(); int array[] = new int[n]; for (int i = 0; i < n; ++i) { array[i] = sc.nextInt(); } Solution ob = new Solution(); int ans[] = ob.help_classmate(array,n); for (int i=0; i<n; i++) System.out.print(ans[i]+" "); System.out.println(); t--; } } } class Solution { public static int[] help_classmate(int arr[], int n) { // Your code goes here Stack<Integer> st=new Stack<>(); int[] ans=new int[n]; Arrays.fill(ans,-1); for(int i=0 ; i<n ;i++){ while(st.size()>0 && arr[i]<arr[st.peek()]){ int tbf=st.pop(); ans[tbf]=arr[i]; } st.push(i); } return ans; } } |